package game;


import java.io.IOException;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import controllers.PlayerController;

import paintball.PaintballGame;
import server.Server;
import vision.Vision;
import ballpool.BallpoolGame;
import calibration.CalibrateScreen;
import chasegame.ChaseGame;
import credits.Credits;

public class MainGame extends StateBasedGame {
	//List with games to vote on
	// The available games to vote for
	public static int[] votelist = null;
	
	//Whether it's a first startup
	// Is this the first state or not?
	public boolean firstState = true;
	
	//The Vision part
	// Vision receiver server
	public Vision vision;
	
	/**
	 * Main method to start the game.
	 * @param args
	 * @throws SlickException
	 */
	public static void main(String[] args) throws SlickException{
		AppGameContainer app = new AppGameContainer(new MainGame("Project Twisty!"));
		app.setTargetFrameRate(Constants.targetFPS);
        app.setDisplayMode(Constants.screenWidth, Constants.screenHeight, Constants.fullscreen);
        app.start();
	}
	
	/**
	 * Class constructor.
	 * @param gameName	The name for the game.
	 */
	public MainGame(String gameName){
		super(gameName);
		Server s = new Server();
		vision = new Vision();
		addGameStates();
		
		try {
			s.start();
			vision.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Add game states to the game.
	 */
	private void addGameStates() {
		this.addState(new VotingMenu(vision));
		this.addState(new PaintballGame(vision));
		this.addState(new ChaseGame(vision));
		this.addState(new BallpoolGame(vision));
		this.addState(new Credits(vision));
		this.addState(new CalibrateScreen(vision));
	}
	
	/**
	 * Initializes the votelist
	 * @param game1	First game
	 * @param game2	Second game
	 * @param game3	Third game
	 */
	public void SetVoteGames ( int game1, int game2, int game3){
		MainGame.votelist = new int[]{game1, game2, game3};
	}
	
	@Override
	public void initStatesList(GameContainer gc) throws SlickException{
		this.getState(Constants.votingMenu).init(gc, this);
		this.getState(Constants.paintballGame).init(gc,this);
		this.getState(Constants.chaseGame).init(gc, this);
		this.getState(Constants.ballpoolGame).init(gc, this);
		this.getState(Constants.credits).init(gc, this);
		this.getState(5).init(gc, this);
		this.enterState(Constants.votingMenu);
	}
	
	@Override
	public void enterState(int state){
		if(firstState){
			firstState = false;
			super.enterState(state);
		}else{
			super.enterState(state, new FadeOutTransition(Color.black, 1000), new FadeInTransition(Color.black, 1000) );
		}
		
		PlayerController.currentGameState = state;
		try{
			if(state == Constants.votingMenu){
				this.SetVoteGames(Constants.paintballGame, Constants.chaseGame, Constants.credits);
				if (PlayerController.getPlayers().size() > 0){
					for(Player p : PlayerController.getPlayers()){
						// TO DO: Create constant to get Constants.VOTING (=2)
						p.send(2);						
					}
				}
			}
		
			if (PlayerController.getPlayers().size() > 0){
				for(Player p : PlayerController.getPlayers()){
					// TO DO: Create constant to get Constants.VOTING (=2)
					p.send(4);						
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
}
